var ctx;

function setCtx(ctx){
	this.ctx = ctx;
}

$(function(){
	$.post("/seller/orderReport/orderEchartsShow?timestamp=" + Date.parse(new Date()),
			{days:7,type:1},function(data){
				data = JSON.parse(data);
				var dates = [];
				for(var i = 0; i<data.obj[0][0].length;i++){
					dates.push(data.obj[0][0][i].substr(5));
				}
				var myChart = echarts.init(document.getElementById('echartsShow'));
				var option = {
					    tooltip: {
					        trigger: 'axis'
					    },
//					    legend: {
//					        data:['已完成订单','已取消订单']
//					    },
					    grid: {
					        left: '3%',
					        right: '4%',
					        bottom: '3%',
					        containLabel: true
					    },
					    xAxis: {
					        type: 'category',
					        boundaryGap: false,
					        data: dates
					    },
					    yAxis: {
					        type: 'value'
					    },
					    series: [
					        {
					            name:'已完成订单',
					            type:'line',
					            stack: '总量',
					            itemStyle : {  
			                        normal : {  
			                        	color:'#32d2c6',
			                            lineStyle:{  
			                                color:'#32d2c6'  
			                            }  
			                        }  
			                    },  
					            data:data.obj[1][0]
					        },
					        {
					            name:'已取消订单',
					            type:'line',
					            stack: '总量',
					            itemStyle : {  
			                        normal : { 
			                        	color:'#f8a20f',
			                            lineStyle:{  
			                                color:'#f8a20f'  
			                            }  
			                        }  
			                    },  
					            data:data.obj[2][0]
					        }
					    ]
					};


				myChart.setOption(option);
	})
	
})

function shopStateChange(id,state){
	if(state == 2){//正常营业改为暂停营业
		var message = "确定暂停营业？";
	}
	if(state == 4){//暂停营业改为正常营业
		var message = "确定开始营业？";
	}
	layer.confirm(message, {
		icon : 3,
		title : '提示'
	}, function(index) {
		$.ajax({
			type : "POST",
			url : ctx + "/home/UpdateShopState",
			cache : false,
			dataType : 'json',
			data:{
				id : id,
				state : state
			},
			success : function(result) {
				if (result.success) {
					layer.msg("操作成功", {
						icon : 1,
						time : 1000
					// 2秒关闭（如果不配置，默认是3秒）
					}, function() {
						loadContent("/home/homePage");
					});
				} else {
					layer.msg("操作失败", {
						icon : 2
					});
				}
			}
		});
		layer.close(index);
	});
}

//function addGoods(){
//	loadContent("/home/editGoodsInfo");
//}

function changeState(orderId,orderstate,orderType){
	switch(orderstate){
		case '1':
			if(orderType == 1){
				//点击确认，商品需配送，订单不自提，跳转待发货
				orderstate = 2;
			}
			if(orderType == 2){
				//点击确认，商品需配送且订单自提或者商品不需配送，跳转待消费
				orderstate = 9;
			}
			if(orderType == 3){
				//点击拒绝，跳转退款已拒绝
				orderstate = 7;
			}
			break;
			
		case '2':	
			orderstate = 3;
			break;
		case '6':	
			if(orderType == 4){
				//退款申请同意后，状态改为待退款订单（即退款已确认）
				orderstate = 7;
			}
			if(orderType == 5){
				//退款申请拒绝后，状态改为待商家确认
				orderstate = 1;
			}
			break;
			
		case '10':	
			if(orderType == 6){
				//退款货申请同意后，状态改为待退货订单（即退货已确认）
				orderstate = 11;
			}
			if(orderType == 7){
				//退款货申请拒绝后，状态改为已完成订单
				orderstate = 4;
			}
			break;
	}
		
	if(orderId != null && orderstate != null){
		$.get(ctx + "/order/updateOrderState",{
			id: orderId,
			state: orderstate	
		},function(data){
			data = JSON.parse(data);
			if(data.success){
				layer.msg("操作成功",{
					icon: 1,
					time: 2000 //2秒关闭（如果不配置，默认是3秒）
				},function(){
					loadContent("/home/homePage");
				});
			}else{
				layer.msg(data.msg,{
					icon: 2,
					time: 2000 //2秒关闭（如果不配置，默认是3秒）
				});
			}
		});
	}
}